import logging
from google.appengine.ext import db

class Mech(db.Model):
    name      = db.StringProperty(multiline=False)
    tonnage   = db.StringProperty(multiline=False)
    player    = db.StringProperty(multiline=False)
    speed     = db.IntegerProperty()
    dropid    = db.StringProperty(multiline=False)
    mechtype  = db.StringProperty(multiline=False)
    mechmodel = db.StringProperty(multiline=False)
    loadout   = db.StringProperty(multiline=False)
    lance     = db.StringProperty(multiline=False)
    starter   = db.IntegerProperty()
    uav       = db.IntegerProperty()
    ecm       = db.IntegerProperty()
    bap       = db.IntegerProperty()
    artemis   = db.IntegerProperty()
    ams       = db.IntegerProperty()
    bombs     = db.IntegerProperty()
    artillery = db.IntegerProperty()
    other     = db.IntegerProperty()
    weapons   = db.ListProperty(str)
    
    def weapons_as_string(self):
        wa = [];
        wo = {};

        for w in self.weapons:
            logging.info("W:"+w)
            if w in wo:
                wo[w] = wo[w] + 1;
            else:
                wa.append(w); 
                wo[w] = 1;                    
        
        ww = [];
        
        for w in wa:

            
            n = wo[w];
                     
            if n > 1:
                ww.append(str(n) + " " + w);
            else:
                ww.append(w);                    
        
        if len(ww) > 0:
            r = ""
            
            # for some reason ", ".join(ww) fails
            for wp in ww:
                r = wp + ", " + r
            
            r = r[:-2]
            
            return r
        else:
            return ""